<?php

namespace app\user\logic;

use app\model\PigWeightUp;

class FeedMeatRatioLogic
{
    /**
     * @Description 料肉比统计
     * @author: zj
     * @param array $param
     * @time 2024/03/27 16:54:43
     * @return array
     */
    public function statistic(array $param)
    {
        $factory_id = request()->user_info['factory_id'];

        $endTime = strtotime(date('Y-m-d 00:00:00',strtotime($param['record_time_end'])));
        $startTime = strtotime(date('Y-m-d 00:00:00',strtotime($param['record_time_start'])));
        $days = ceil(($endTime-$startTime)/86400);

        $data = [];

        for ($i = 0;$i <= $days;$i++) {
            $start_time = $startTime + $i * 86400;
            $end_time = $start_time + 86399;

            $fmr = $this->getFMRData($factory_id,$start_time,$end_time,$param['barn_id'],$param['fence_id'],$param['pig_id']);

            $data[] = [
                'date' => date('Y-m-d',$start_time),
                'fmr' => round($fmr,2)
            ];
        }

        return $data;
    }

    /**
     * @Description 料肉比 比较
     * @author: zj
     * @param array $param
     * @time 2024/03/27 18:06:15
     * @return array
     */
    public function compare(array $param)
    {
        $factory_id = request()->user_info['factory_id'];

        $current = strtotime(date('Y-m-d H:i:s',request()->time()));

        $todayEnd = $current + 86399;
        $todayFmr = $this->getFMRData($factory_id,$current,$todayEnd,(int)$param['barn_id'],(int)$param['fence_id'],(int)$param['pig_id']);

        $yesterdayStart = $current-86400;
        $yesterdayEnd = $yesterdayStart + 86399;
        $yesterdayFmr = $this->getFMRData($factory_id,$yesterdayStart,$yesterdayEnd,(int)$param['barn_id'],(int)$param['fence_id'],(int)$param['pig_id']);

        $twoDaysStart = $current-2*86400;
        $twoDaysEnd = $twoDaysStart + 86399;
        $twoDaysFmr = $this->getFMRData($factory_id,$twoDaysStart,$twoDaysEnd,(int)$param['barn_id'],(int)$param['fence_id'],(int)$param['pig_id']);

        $threeDaysStart = $current-3*86400;
        $threeDaysEnd = $threeDaysStart + 86399;
        $threeDaysFmr = $this->getFMRData($factory_id,$threeDaysStart,$threeDaysEnd,(int)$param['barn_id'],(int)$param['fence_id'],(int)$param['pig_id']);

//        $data = [];
//        $days = 13;
//        for ($i = 0;$i < $days;$i++) {
//            $start_time = $current - $i*86400;
//            $end_time = $start_time + 86399;
//            $currentFmr = $this->getFMRData($factory_id,$start_time,$end_time,(int)$param['barn_id'],(int)$param['fence_id'],(int)$param['pig_id']);
//
//            $oneDayAgoStart = $start_time - 86400;
//            $oneDayAgoEnd = $oneDayAgoStart + 86399;
//            $oneDayAgoFmr = $this->getFMRData($factory_id,$oneDayAgoStart,$oneDayAgoEnd,(int)$param['barn_id'],(int)$param['fence_id'],(int)$param['pig_id']);
//
//            $data[$i] = [
//                'day_ago' => $i,
//                'fmr' => $currentFmr,
//                'compare' => $this->compareFmr($oneDayAgoFmr,$currentFmr)
//            ];
//        }
//        return $data;

        return [
            'today_fmr' => $todayFmr,
            'today_compare' => $this->compareFmr($yesterdayFmr,$todayFmr),
            'yesterday_fmr' => $todayFmr,
            'yesterday_compare' => $this->compareFmr($twoDaysFmr,$yesterdayFmr),
            'two_day_ago_fmr' => $todayFmr,
            'two_day_ago_compare' => $this->compareFmr($threeDaysFmr,$twoDaysFmr)
        ];
    }

    /**
     * @Description 获取料肉比数据
     * @author: zj
     * @param int $factory_id
     * @param int $start_time
     * @param int $end_time
     * @param int $barn_id
     * @param int $fence_id
     * @param int $pig_id
     * @time 2024/03/27 17:14:52
     * @return int
     */
    public function getFMRData(int $factory_id,int $start_time,int $end_time,int $barn_id = 0,int $fence_id = 0,int $pig_id = 0)
    {
        $model = new PigWeightUp();
        $query = $model->alias('pwu')
            ->join('pig p','pwu.out_pig_id = p.out_id','left')
            ->join('fence f','p.out_fence_id = f.out_id','left')
            ->where('pwu.factory_id',$factory_id)
            ->where('pwu.record_time','>=',$start_time)
            ->where('pwu.record_time','<=',$end_time)
            ->where('pwu.delete_time',$model::IS_DELETE_NO);
        if ($barn_id) {
            $query->where('f.out_barn_id',$barn_id);
        }
        if ($fence_id) {
            $query->where('p.out_fence_id',$fence_id);
        }
        if ($pig_id) {
            $query->where('pwu.out_pig_id',$pig_id);
        }

        return $query->avg('pwu.fmr') ?? 0;
    }

    /**
     * @Description 比较料肉比
     * @author: zj
     * @param $fmr1
     * @param $fmr2
     * @time 2024/03/27 17:35:10
     * @return int
     */
    public function compareFmr($fmr1,$fmr2)
    {
        if ($fmr1 > $fmr2) {
            return 1;
        } else if ($fmr1 < $fmr2){
            return 2;
        } else {
            return 0;
        }
    }
}